import AsyncStorage from '@react-native-async-storage/async-storage';

/** 保存信息 */
export const saveUserInfo = async (userInfo: Record<string, any>) =>{
    await AsyncStorage.setItem('userInfo', JSON.stringify(userInfo));
};

export const getUserInfo = async () => {
    return JSON.parse(<string>await AsyncStorage.getItem('userInfo'));
}

export const saveAccessToken = async (accessToken: string) => {
    await AsyncStorage.setItem("accessToken", accessToken);
};

export const saveRefreshToken = async (refreshToken: string) => {
    await AsyncStorage.setItem("refreshToken", refreshToken);
};

/** 更新单个 Token（比如刷新 accessToken） */
export const updateAccessToken = async (newAccessToken) => {
    await AsyncStorage.setItem("accessToken", newAccessToken);
};

/** 清空（退出登录时调用） */
export const loginOut = async () => {
    await AsyncStorage.multiRemove(["userInfo", "accessToken", "refreshToken"]);
};